package com.ctshk.rpc.order.tour.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.tour.code.IsDeletedCode;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristQuitListReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristRefundListReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

/**
 * <p>
 * 系列团游客退团 Provider 接口
 * </p>
 *
 * @author 谭响
 * @since 2021-01-18
 */
public class SeriesOrderTouristQuitProvider {

    public String selectListBySelective(IPage<SeriesOrderTouristRefundListReq> pageParam, @Param("req") SeriesOrderTouristQuitListReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsotq.id , tsoti.order_id, tsoti.schedule_id, tsoti.id as touristId, ");
        sb.append("tsoti.cn_surname, tsoti.cn_name, ");
        sb.append("tsoti.total_paid_in, ");
        sb.append("tsotq.deduction_amount, tsotq.refund_amount, tsotq.discount_amount, tsotq.refund_type_id, tsotq.refund_type, ");
        sb.append("tsotq.refund_account, tsotq.quit_reason, tsotq.remark, ");
        sb.append("tsotq.gmt_create, tsotq.create_id, tsotq.refund_status ");
        sb.append("FROM t_series_order_tourist_info tsoti ");
        sb.append("LEFT JOIN ( SELECT temp.id, temp.tourist_id, temp.deduction_amount, temp.refund_amount, temp.discount_amount, " +
                "temp.refund_type_id, temp.refund_type, temp.refund_account, temp.quit_reason, temp.remark, " +
                "temp.gmt_create, temp.create_id, temp.refund_status, temp.is_deleted " +
                "FROM t_series_order_tourist_quit temp WHERE temp.is_deleted = 0 AND temp.order_id = #{req.orderId} ORDER BY temp.gmt_create desc ) tsotq ON tsotq.tourist_id = tsoti.id ");
        sb.append("where tsoti.is_full_info = 1 ");
        sb.append("and tsoti.order_id = #{req.orderId} ");
        sb.append("and tsoti.is_deleted=" + IsDeletedCode.NO.getCode() + " ");
        if (req.getRefundStatus() != null) {
            sb.append("and tsotq.refund_status = #{req.refundStatus} ");
        }
        if (StringUtils.isNotBlank(req.getCondition1())) {
            sb.append("and ((tsoti.cn_surname || tsoti.cn_name) like concat('%',#{req.condition1},'%') " +
                    "OR (tsoti.en_surname || tsoti.en_name) like concat('%',#{req.condition1},'%')) ");
        }
        return sb.toString();
    }


}
